package cn.com.fattiger.framework.cms.admin.common.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import cn.com.fattiger.framework.cms.admin.common.dao.VersionRepository;
import cn.com.fattiger.framework.cms.admin.common.model.Version;
import cn.com.fattiger.framework.cms.admin.common.service.VersionService;

@Service
public class VersionServiceImpl implements VersionService{

	@Autowired
	private VersionRepository versionRepository;
	@Override
	public void save(Version entity) {
		versionRepository.save(entity);
	}

	@Override
	public void delete(Integer id) {
		versionRepository.delete(id);
	}
	
	@Override
	public Version get(Integer id) {
		return versionRepository.findOne(id);
	}
	
	@Override
	public Integer getCode(String appKey,Integer versionCode){
		if(versionCode != null){
			return versionRepository.findPatchCode(appKey, versionCode);
		}
		return versionRepository.findVersionCode(appKey);
	}

	@Override
	public Page<Version> list(Integer page, Integer pageSize, final Map<String, String> condition) {
		Pageable pageable = new PageRequest(page, pageSize, Direction.DESC, "appKey", "createTime");
		if (condition.keySet().size() > 0) {
			Specification<Version> spec = new Specification<Version>() {
				@Override
				public Predicate toPredicate(Root<Version> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
					List<Predicate> list = new ArrayList<Predicate>();
					if (condition.containsKey("appName")) {
						Path<String> appNamePath = root.get("appName");
						String appName = condition.get("appName");
						list.add(cb.like(appNamePath,"%"+appName+"%"));
					}
					if (condition.containsKey("fileType")) {
						Path<String> fileTypePath = root.get("fileType");
						Integer fileType = Integer.parseInt(condition.get("fileType"));
						list.add(cb.equal(fileTypePath,fileType));
					}
					if (condition.containsKey("status")) {
						Path<String> statusPath = root.get("status");
						Integer status = Integer.parseInt(condition.get("status"));
						list.add(cb.equal(statusPath,status));
					}
					Predicate[] predicates = new Predicate[list.size()];
					for(int i=0;i<list.size();i++){
						predicates[i] = list.get(i);
					}
					cq.where(predicates);
					return null;
				}
			};
			return versionRepository.findAll(spec, pageable);
		}
		return versionRepository.findAll(pageable);
	}

	@Override
	public void updateStauts(Integer id, Integer status) {
		versionRepository.updateStauts(status, id);
	}

	@Override
	public void updateChangeLog(Integer id, String changeLog) {
		versionRepository.updateChangeLog(changeLog, id);
	}

}
